package com.trafficsys.modules.loginAndRegister.controller;

import com.trafficsys.modules.entity.DriveCard;
import com.trafficsys.modules.entity.Police;
import com.trafficsys.modules.entity.TravelCard;
import com.trafficsys.modules.loginAndRegister.service.DriverService;
import com.trafficsys.modules.loginAndRegister.service.PoliceService;
import com.trafficsys.modules.loginAndRegister.util.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author Zhao Bin
 * @Date 2021/9/28 13:24
 * @Version 1.0
 */
@Controller
@RequestMapping("/api")
public class PoliceController {
    @Autowired
    private PoliceService policeService;
    @Autowired
    private Limit limit;
    /*********************************************   以下管理员功能  *********************************************************
     * 警员员登录    管理员功能
     * post:
     * http://localhost:8080/api/policeLogin?&phone=15730593398&password=15730593398
     * json:
     * http://localhost:8080/api/policeLogin
     * {
     *     "cardId":"500235199805059270",
     *     "password":"15730593398"
     * }
     */
    @PostMapping(value = "/policeLogin", consumes = "application/json")
    @ResponseBody
    public HashMap<String,Object> policeLogin(@RequestBody Police police, HttpServletRequest request){
        HashMap<String,Object> map=new HashMap<>();
        //限制访问频率
        if(limit.limit(request).get("state").equals("Pass")){
            map=policeService.policeLogin(police);
            return map;
        }else{
            map.put("info","您的访问频率过高,请在一分钟后访问");
        }
        return map;
    }

    /**
     * 添加驾驶证方法   管理员功能
     * post:
     * http://localhost:8080/api/addDriveCard?driveCardId=500235199805059270&cardId=500235199805059270&nationality=中国&clazz=C1&department=成都交警总队&score=12&state=在用
     * json:
     {
     "driveCardId":"500235199805059270A1",
     "cardId":"500235199805059270",
     "nationality":"中国",
     "clazz":"A1",
     "department":"成都交警总队",
     "score":"12",
     "state":"在用"
     }
     */
    @PostMapping(value = "/addDriveCard", consumes = "application/json")
    @ResponseBody
    public HashMap<String,Object> addDriveCard(@RequestBody DriveCard driveCard){
        return policeService.addDriveCard(driveCard);
    }
    /**
     * 添加行驶证方法   管理员功能
     * http://localhost:8080/api/addTravelCard?travelCarId=500235199805059270渝A9999&carNo=渝A 99999&carClazz=大型SUV&cardId=500235199805059270&userNature=民用&brandModel=保时捷Utrl&vin=45347542355236&engineNo=6754345&fileNo=3442332&state=在用
     {
     "travelCarId":"500235199805059270渝A11111",
     "carNo":"渝A 11111",
     "carClazz":"大型SUV",
     "cardId":"500235199805059270",
     "userNature":"民用",
     "brandModel":"保时捷Utrl",
     "vin":"45347542355236",
     "engineNo":"6754345",
     "fileNo":"3442332",
     "state":"在用"
     }
     */
    @PostMapping(value = "/addTravelCard", consumes = "application/json")
    @ResponseBody
    //添加行驶证方法   管理员功能
    public HashMap<String, Object> addTravelCard(@RequestBody TravelCard travelCard){
        return policeService.addTravelCard(travelCard);
    };
    /**
     * 添加警员    管理员功能
     * post:
     * http://localhost:8080/api/addPolice?cardId=500235199805059270&name=赵彬&phone=15730593398&password=15730593398
     * json:
     {
     "cardId":"500235199805059271",
     "name":"赵彬",
     "phone":"15730593398",
     "password":"15730593398"
     }
     */
    @PostMapping(value = "/addPolice", consumes = "application/json")
    @ResponseBody
    public HashMap<String,Object> addPolice(@RequestBody Police police){
        return policeService.addPolice(police);
    }
    /**
     *  根据phone查询到警员信息
     */
    @GetMapping("/getPoliceByPhone/{phone}")
    @ResponseBody
    public HashMap<String,Object> getPoliceByPhone(@PathVariable String phone){
        return policeService.getPoliceByPhone(phone);
    }
    /**
     * 查询权限
     */
    @GetMapping("/policeSelectPower")
    @ResponseBody
    public HashMap<String,Object> policeSelectPower(String cardId){
        return policeService.policeSelectPower(cardId);
    }

}
